//
//  GYHOAuthViewController.swift
//  sinaWeibo_18
//
//  Created by 高勇华 on 15/10/6.
//  Copyright © 2015年 高勇华. All rights reserved.
//

import UIKit

import SVProgressHUD

class GYHOAuthViewController: UIViewController,UIWebViewDelegate {

    
//    let client_id = "4148423446"
//    let redirect_uri = "http://www.baidu.com"
//    
//    let pageURL = "https://api.weibo.com/oauth2/authorize"
//    
    var webView : UIWebView{
        get {
        return view as!UIWebView
            
        }
    
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadOAuthVirewebpage()
        
        webView.delegate = self
       
    }

   private func loadOAuthVirewebpage(){
    
//        let urlString = pageURL + "?" + "client_id=" + client_id + "&" + "redirect_uri=" + redirect_uri
//    
       // print(urlString)
        let url = GYHNetWorkTool.sharedTools.oauthUrl()
    
    
    webView.loadRequest(NSURLRequest(URL: url))
    
    
//    UIColor.blackColor()
    
    
    }
    
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        /*
        https://api.weibo.com/oauth2/authorize?client_id=4148423446&redirect_uri=http://www.baidu.com
        https://api.weibo.com/oauth2/authorize
        https://api.weibo.com/oauth2/authorize#
        https://api.weibo.com/oauth2/authorize
        http://www.baidu.com/?code=db293b6a4c78e1df8097c2714ebc9476
        */
        
//        print(request)
        //absolute 绝对的
        let urlString = request.URL!.absoluteString
        
        //过滤掉没有用的url请求
        if urlString.hasPrefix("https://api.weibo.com/"){
        
        return true
        }
//        
        if !urlString.hasPrefix(GYHNetWorkTool.sharedTools.WB_redirect_uri){
        
        return false
        }
        
        
//       print(urlString)
       //request.URL!.query 拿到url请求参数=的值
        //query  询问
//        let query = request.URL!.query
//        
//        print(query)
//
        if let query = request.URL?.query{
        
        let codeStr = "code="
        
            let code = (query as NSString).substringFromIndex(codeStr.characters.count)
            print(code)
            
            loadAccessToken(code)
        
        }
        
        return false
        
    }
    
    private func loadAccessToken(code:String){
    GYHNetWorkTool()
    GYHNetWorkTool.sharedTools.loadAccessToken(code) { (dict, error) -> () in
        if error != nil{
            
        SVProgressHUD.showWithStatus("你的网络不给力")
        return
        }
        
        let account = GYHUserAccount(dict: dict!)
        
        account.loadUserInfo({ (error) -> () in
            
            if error != nil {
            print("网络请求错误")
            return
            }
            
            self.cilckShut(self)
            
        })
        
        
        }
    
    }
    
    
    
    
    
   //点击关闭item
    @IBAction func cilckShut(sender: AnyObject) {
        SVProgressHUD.dismiss()
        
        self.dismissViewControllerAnimated(true, completion: nil)
        
        
    }

    //点击自动填充
    
       @IBAction func cilckStuff(sender: AnyObject) {
   
    let jsString = "document.getElementById('userId').value = '13122204362';document.getElementById('passwd').value = 'gaoyonghua'"
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
        
        
    }
   

    //开始加载webView的时候调用
  func webViewDidStartLoad(webView: UIWebView){
    
        SVProgressHUD.show()
    
    
    }
    
    
    //webView消失的时候调用
   func webViewDidFinishLoad(webView: UIWebView)
       {
    
        let jsString = "document.title"
        
        let wedTitle =  webView.stringByEvaluatingJavaScriptFromString(jsString)
        
        title = wedTitle
        
        
        SVProgressHUD.dismiss()
        
    }
    
    
        
    
    
}
